<template>

  <div>
    <div class="card" style="margin-bottom: 5px">
      <el-input style="width: 240px;margin-right: 10px" v-model="data.name" placeholder="请输入名称" prefix-icon="search"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-button type="primary" @click="handleAdd">新增</el-button>
      <el-button type="warning" @click="delBatch">批量删除</el-button>
<!--      <el-button type="info">导入</el-button>-->
<!--      <el-button type="success">导出</el-button>-->
    </div>
    <div class="card" style="margin-bottom: 5px">
      <el-table :data="data.tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>
        <el-table-column label="账号" prop="username"/>
        <el-table-column label="头像">
          <template #default="scope">
            <img v-if="scope.row.avatar" :src="scope.row.avatar" alt="" style="display:block;width: 40px;height: 40px;border-radius: 50%">
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="name"/>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button @click="handleUpdate(scope.row)" type="primary" :icon="Edit" circle></el-button>
            <el-button @click="del(scope.row.id)" type="danger" :icon="Delete" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 15px;">
        <el-pagination
            @size-change="load"
            @current-change="load"
            v-model:current-page="data.pageNum"
            v-model:page-size="data.pageSize"
            :page-sizes="[5,10,15,20]"
            background
            layout="total, size,prev, pager, next, jumper"
            :total="data.total"
        />
      </div>
    </div>
    <el-dialog title="管理员信息" v-model="data.formVisible"  width="500" destroy-on-close>
      <el-form ref="formRef" :rules="data.rules" :model="data.form" label-width="80px" style="padding-right: 50px;padding-top: 20px;">
        <el-form-item label="账号" prop="username" >
          <el-input :disabled="data.form.id" v-model="data.form.username" autocomplete="off" placeholder="请输入账号" />
        </el-form-item >
        <el-form-item label="头像"  >
          <el-upload
              action="http://localhost:8080/files/upload"
              list-type="picture"
              :on-success="handleAvatarSuccess"
          >
            <el-button type="primary">上传头像</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="名称" prop="name" >
          <el-input v-model="data.form.name" autocomplete="off" placeholder="请输入名字" />
        </el-form-item >
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.formVisible = false">取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>


<script setup>
import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {Delete, Edit} from "@element-plus/icons-vue";
import {handleCurrentChange} from "element-plus/es/components/tree/src/model/util";

const data=reactive({
  name:null,
  tableData:[],
  pageNum:1,
  pageSize:10,
  total:0,
  formVisible:false,
  form:{},
  ids:[],
  rules:{
    username:[
      {required:true,message:'请输入账号',trigger:'blur'}
    ],
    name:[
      {required:true,message:'请输入名称',trigger:'blur'}
    ]
  }
})

const formRef = ref()

const handleAvatarSuccess=(res)=>{
  console.log(data)
  data.form.avatar=res.data;
}

const load=()=>{
  request.get('/admin/selectPage',{
    params:{
      pageNum:data.pageNum,
      pageSize:data.pageSize,
      name:data.name
    }
  }).then(res=>{

    data.tableData=res.data.list;
    data.total=res.data.total;
  })
}
load()

const reset = ()=>{
  data.name=null
  load()
}

const handleAdd=()=>{
  data.formVisible=true;
  data.form={}
}

const save=()=>{
  formRef.value.validate((valid)=>{
    if(valid){
      data.form.id ? update() : add()
    }
  })



}
const add=()=>{
  request.post('/admin/add',data.form).then(res=>{// 新增对象没有id
    if(res.code==='200'){
      data.formVisible=false
      ElMessage.success('操作成功')
      load()//新增后从新加载
    }else{
      ElMessage.error(res.msg)
    }
  })
}
const update=()=>{
  request.put('/admin/update',data.form).then(res=>{//修改对象没有id
    if(res.code==='200'){
      data.formVisible=false
      ElMessage.success('修改成功')
      load()//新增后从新加载
    }else{
      ElMessage.error(res.msg)
    }
  })
}
const handleUpdate=(row)=>{
    data.form=JSON.parse(JSON.stringify(row));//深拷贝，不会影响
    data.formVisible=true;
}

const del=(id)=>{
  ElMessageBox.confirm('删除后无法恢复，请确认','删除确认',{type:'warning'}).then(()=>{
    request.delete('/admin/delete/' +id).then(res=>{
      if(res.code==='200'){
        data.formVisible=false
        ElMessage.success('删除成功')
        load()//删除后更新
      }else{
        ElMessage.erroe(res.msg)
      }
    })
  }).catch()
}

const handleSelectionChange = (rows)=>{//返回所有选中的行

  //从选中的行数里面取出所有行的id组成一个新的数组
  data.ids=rows.map(row=>row.id)
}

const delBatch=()=>{
  if(data.ids.length===0){
    ElMessage.error('请你选择值')
    return
  }
  ElMessageBox.confirm('删除后无法恢复，请确认','删除确认',{type:'warning'}).then(()=>{
    request.delete('/admin/deleteBatch',{data:data.ids}).then(res=>{
      if(res.code==='200'){
        data.formVisible=false
        ElMessage.success('删除成功')
        load()//删除后更新
      }else{
        ElMessage.erroe(res.msg)
      }
    })
  }).catch()

}
</script>

<style scoped>
/* 表格数据更新动画 */
:deep(.el-table__body-wrapper) {
  transition: all 0.3s ease;
}

:deep(.el-table__row) {
  animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>